package com.handu.luna.core.domain.goods;

import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.handu.luna.core.domain.BaseRepository;
import com.handu.luna.core.domain.support.GoodsGroupInfo;

public interface GoodsAttributeRepository  extends JpaRepository<GoodsAttribute,Long>, BaseRepository<GoodsAttribute>,JpaSpecificationExecutor<GoodsAttribute>{
	@Query("select b from GoodsAttribute b where b.goodsId =:goodsId order by id asc")
	List<GoodsAttribute> findByGoodsId(@Param("goodsId") Long goodsId);
	
	@Query("from GoodsAttribute b where b.attributeValues =:attributeValues and b.goodsId =:goodsId")
	GoodsAttribute findByAttributeValues(@Param("attributeValues") String attributeValues,@Param("goodsId") long goodsId);
	
	@Query("select distinct attributeId from GoodsAttribute where attributeValues in :ids")
	List<Long> findExcludedAttributeId(@Param("ids")String[] searchAttributes);
	
	@Query("select distinct ga.goodsId from GoodsAttribute ga where ga.attributeValues = :attributeValues")
	Set<Long> findGoodsIdByAttributeValues(@Param("attributeValues")String attributeValues);
	
	@Query("select distinct ga.goodsId from GoodsAttribute ga where ga.attributeValues = :attributeValues and ga.goodsId in :ids")
	Set<Long> findGoodsIdByAttributeValuesAndGoodsId(@Param("attributeValues")String attributeValues,@Param("ids")Collection<Long> ids);
	
	@Query("select NEW com.handu.luna.core.domain.support.GoodsGroupInfo(max(b.attributeId),max(b.attributeName),b.attributeValues,COUNT(DISTINCT b.goodsId)) from GoodsAttribute b where b.attributeId not in ?1 and b.goodsId in ?2 group by b.attributeValues")
	List<GoodsGroupInfo> goodsAttributeGroup(List<Long> excludedAttributeId,List<Long> goodsIds);
	
	@SuppressWarnings("rawtypes")
	@Query("select NEW com.handu.luna.core.domain.support.GoodsGroupInfo(max(b.attributeId),max(b.attributeName),b.attributeValues,COUNT(DISTINCT b.goodsId)) from GoodsAttribute b where b.goodsId in ?1 group by b.attributeValues")
	List<GoodsGroupInfo> goodsAttributeGroup(Collection goodsIds);
	
	@Query("select g.attributePic from GoodsAttribute g where g.attributeId = (select a.id from Attribute a where a.name='颜色') and g.goodsId = :goodsId and g.attributePic is not null and g.attributePic <> '' order by g.createdDate desc")
	Page<String> findGoodsAttributePic(@Param("goodsId")long goodsId,Pageable pageable);
	
    /*******************************************************************************************************/
    //商品详情页
    @Query("select g from GoodsAttribute g where g.goodsId = ?1 and g.attributeName = ?2")
    GoodsAttribute findGoodsAttributeByGoodsIdAndName(long goodsId,String attributeName);


    /*******************************************************************************************************/
	
}
